package com.hsq.commons.util.db.ibatisExecutor;


public class OracleDialect implements Dialect {

	protected static final String SQL_END_DELIMITER = ";";


	public String getLimitString(String sql, int offset, int limit) {
		sql = trim(sql);
		StringBuffer sb = new StringBuffer();
		sb.append(" select * from ( select row_.*, rownum rownum_ from ( ")
        	.append(sql)
        	.append(" ) row_ where rownum <= ")
        	.append( limit+offset )
        	.append( ") where rownum_ > ")
        	.append( limit );
		return sb.toString();
	}

	public boolean supportsLimit() {
		return true;
	}

	private String trim(String sql) {
		sql = sql.trim();
		if (sql.endsWith(SQL_END_DELIMITER)) {
			sql = sql.substring(0, sql.length() - 1
					- SQL_END_DELIMITER.length());
		}
		return sql;
	}

}